package com.ruoyi.eqmonitor.controller;

import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.eqmonitor.dtos.OptDeviceHistoryDto;
import com.ruoyi.eqmonitor.service.IOptDeviceMonitorDataService;
import com.ruoyi.eqmonitor.utils.Constant;
import com.ruoyi.eqmonitor.vo.DeviceMonitorParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备监测数据 服务
 */
@RestController
@RequestMapping("/monitorData")
public class OptDeviceMonitorDataController extends BaseController {

    @Resource
    private IOptDeviceMonitorDataService iOptDeviceMonitorDataService;

    /**
     * 污水大屏地图点击事件 -- 查询曲线
     * @param deviceCode
     * @return
     */
    @ApiOperation("污水大屏地图点击事件 -- 查询曲线")
    @GetMapping("/deviceMonitorChartAnalyse")
    public AjaxResult deviceMonitorChartAnalyse(@RequestParam("deviceCode") String deviceCode,@RequestParam(value = "type", required = false, defaultValue = "normal")String type) {
        return success(iOptDeviceMonitorDataService.deviceMonitorChartAnalyse(deviceCode,type));
    }
    
    /**
     * 获取每个设备监测的所有指标
     * @return
     */
    @ApiOperation("获取每个设备监测的所有指标")
    @GetMapping("/deviceMonitorParams")
    public AjaxResult deviceMonitorParams() {
        Constant.EquipmentTypeMonitorInfo[] values = Constant.EquipmentTypeMonitorInfo.values();
        List<DeviceMonitorParams> result = new ArrayList<>();
        for (Constant.EquipmentTypeMonitorInfo equipmentInfo : values) {
            DeviceMonitorParams deviceMonitorParams = new DeviceMonitorParams();
            deviceMonitorParams.setDeviceType(equipmentInfo.getTypeCode());
            deviceMonitorParams.setDeviceTypeName(Constant.EquipmentType.getValueByCode(equipmentInfo.getTypeCode()));
            deviceMonitorParams.setMonitorParams(
                    Arrays.stream(equipmentInfo.getMonitorInfos())
                            .filter(Constant.MonitorInfo::isSave)
                            .map(item -> {
                                DeviceMonitorParams.MonitorParams monitorParams = new DeviceMonitorParams.MonitorParams();
                                monitorParams.setMonitorCode(item.getCode());
                                monitorParams.setDataUnit(item.getUnit());
                                monitorParams.setMonitorName(item.getName());
                                return monitorParams;
                            }).collect(Collectors.toList())
            );
            result.add(deviceMonitorParams);
        }
        return success(result);
    }

    /**
     * 降精度采样 -- 管理后台的设备监测分析使用
     * @param historyDto
     * @return
     */
    @ApiOperation("降精度采样 -- 管理后台的设备监测分析使用")
    @PostMapping("/getDownsampleHistory")
    public AjaxResult getDownsampleHistory(@Validated @RequestBody OptDeviceHistoryDto historyDto) {
        return AjaxResult.success(iOptDeviceMonitorDataService.getDownsampleHistory(historyDto));
    }

    /**
     * 泵站监测数据查询 -- 负荷分析选择泵站点击查询 显示右侧的图表所需要的数据
     */
    @ApiOperation("泵站监测数据查询 -- 负荷分析选择泵站点击查询 显示右侧的图表所需要的数据")
    @GetMapping("/sewagePumpMonitorInfo")
    public AjaxResult sewagePumpMonitorInfo(@RequestParam("pumpIds") Long[] pumpIds) {
        return success(iOptDeviceMonitorDataService.sewagePumpMonitorInfo(ListUtil.of(pumpIds)));
    }

    /**
     * 泵站日累计流量查询 -- 负荷分析选择泵站点击查询 显示右侧表格需要的数据
     */
    @ApiOperation("泵站日累计流量查询 -- 负荷分析选择泵站点击查询 显示右侧表格需要的数据")
    @GetMapping("/sewagePumpCumulativeDischarge")
    public AjaxResult sewagePumpCumulativeDischarge(@RequestParam("pumpIds") Long[] pumpIds) {
        return success(iOptDeviceMonitorDataService.sewagePumpCumulativeDischarge(ListUtil.of(pumpIds)));
    }
}
